<template>
  <div>
    <FormPanel :list="topFilterList" :cols="3" :labelWidth="100" :collapse="false" :isSubmitBtn="false" />
    <FilterTable
      ref="mainTable"
      :columns="tableColumns"
      columnsRef="partInventoryInfo"
      :datakey="'rows'"
      selectionType="single"
      :dataSource="dataSource"
      :onColumnsChange="columns => (this.tableColumns = columns)"
    >
    </FilterTable>
  </div>
</template>
<script>
import { partbuyStockInItemOEM } from '@/api/part/partInventory/partInventory/index.js';
import { dictionary } from '@/utils/dictMixin';
import { byAllInfo } from '@/api/part/allocation';
import { getStorageList, querySysParam } from '@/api/part/common';

export default {
  name: 'partBuyItems',
  props: {
    top: {}
  },
  mixins: [dictionary],
  data() {
    return {
      topFilterList: this.createTopFilterList(),
      tableColumns: this.createTableColumns(),
      fetchParams: {},
      dataSource: [],
      storageDic: [
        { text: 'OEM电', value: 'OEM-ELECTIC' },
        { text: 'OEM油', value: 'OEM-OIL' }
      ] // 仓库字典
    };
  },
  mounted() {
    this.translate(this.top.ownerCode);
    // this.getStorage();
    this.findBuyItems();
  },
  methods: {
    createTopFilterList() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'ownerCode',
          initialValue: '',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'ownerName',
          initialValue: '',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '入库单号',
          fieldName: 'stockInNo',
          initialValue: '',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '业务往来客户',
          initialValue: '',

          fieldName: 'customerCode',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT_NUMBER',
          label: '税率',
          initialValue: 0,

          fieldName: 'tax',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'SELECT',
          label: '入库类型',
          initialValue: '',

          fieldName: 'stockInType',
          itemList: this.createDictList(8088),
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'INPUT',
          label: '货运单号',
          initialValue: '',

          fieldName: 'purchaseCode',
          placeholder: '请输入...',
          disabled: true
        },
        {
          type: 'DATE',
          fieldName: 'createdAt',
          initialValue: '',

          label: '开单日期',
          dateFormat: 'yyyy-MM-dd',
          disabled: true
        }
      ];
    },
    createTableColumns() {
      return [
        {
          title: '序号',
          // sorter: true,
          width: 70,
          dataIndex: 'xuhao',
          sorter: true,
          render: props => {
            return <span>{props.row.$index + 1}</span>;
          },
          sorter: true
        },
        {
          title: '仓库',
          sorter: true,
          minWidth: 150,
          dataIndex: 'STORAGE_CODE',
          // disabled: false,
          // editable: true,
          // defaultEditable: true,
          // editType: 'select',
          // editItems: this.storageDic,
          filter: true,
          filterType: 'checkbox',
          filterItems: [
            { text: 'OEM电', value: 'OEM-ELECTIC' },
            { text: 'OEM油', value: 'OEM-OIL' }
          ],
          dictItems: [
            { text: 'OEM电', value: 'OEM-ELECTIC' },
            { text: 'OEM油', value: 'OEM-OIL' }
          ]
        },
        {
          title: '库位',
          sorter: true,
          width: 150,
          dataIndex: 'STORAGE_POSITION_CODE',
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件代码',
          width: 180,
          sorter: true,
          dataIndex: 'PART_NO',
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '配件名称',
          width: 180,
          sorter: true,
          dataIndex: 'PART_NAME',
          showOverflowTooltip: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '入库数量',
          sorter: true,
          dataIndex: 'IN_QUANTITY',
          precision: 2,
          filter: true,
          filterType: 'number',
          min: 0,
          max: Infinity,
          widht: 130
        },
        // {
        //   title: '签收数量',
        //   sorter: true,
        //   dataIndex: 'SIGN_QUANTITY',
        //   precision: 2,
        //   filter: true,
        //   filterType: 'number',
        //   min: 0,
        //   max: Infinity,
        //   widht: 130
        // },
        // {
        //   title: '不含税单价',
        //   sorter: true,
        //   dataIndex: 'IN_PRICE',
        //   summation: true,
        //   filter: true,
        //   filterType: 'number',
        //   precision: 4
        // },
        // {
        //   title: '不含税金额',
        //   sorter: true,
        //   dataIndex: 'IN_AMOUNT',
        //   precision: 2,
        //   summation: true,
        //   filter: true,
        //   filterType: 'number'
        // },
        {
          title: '含税单价',
          sorter: true,
          dataIndex: 'IN_PRICE_TAXED',
          precision: 2,
          filter: true,
          filterType: 'number'
        },
        {
          title: '含税金额',
          sorter: true,
          dataIndex: 'IN_AMOUNT_TAXED',
          precision: 2,
          summation: true,
          filter: true,
          filterType: 'number'
        }
      ];
    },
    async translate(dealerCode) {
      let name = '';
      let res = await byAllInfo({ dealerCode: dealerCode });
      if (res.resultCode == 200 && res.data && res.data.records && res.data.records.length > 0) {
        name = res.data.records[0].companyName;
      }
      this.topFilterList.find(x => x.fieldName == 'ownerName').initialValue = name;
    },
    async getStorage() {
      const ckRes = await getStorageList();
      const ckDict = ckRes.data.map(x => ({ text: x['STORAGE_NAME'], value: x['STORAGE_CODE'] }));
      this.storageDic = ckDict;
      this.tableColumns.find(x => x.dataIndex == 'STORAGE_CODE').filterItems = this.tableColumns.find(x => x.dataIndex == 'STORAGE_CODE').dictItems = ckDict;
    },
    async findBuyItems() {
      this.topFilterList.map(x => {
        x.initialValue = this.top[x.fieldName];
      });
      let res = await partbuyStockInItemOEM({ stockInNo: this.top.stockInNo, ownerCode: this.top.ownerCode });
      this.dataSource = res.data;
    }
  }
};
</script>
